package chapter14.web;

import java.util.*;

import chapter14.core.*;

import chapter14.domain.*;

import chapter14.manager.*;

import com.softwarementors.extjs.djn.config.annotations.DirectFormPostMethod;
import com.softwarementors.extjs.djn.config.annotations.DirectMethod;

import org.apache.commons.fileupload.FileItem;


public class InAction extends SecurityAction<In> {
    private InManager inManager;
    private ProductManager productManager;

    @Override
    protected InManager getEntityDao() {
        return inManager;
    }

    public void setInManager(InManager inManager) {
        this.inManager = inManager;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    @Override
    @DirectFormPostMethod
    public Object save(Map<String, String> param,
        Map<String, FileItem> files) throws Exception {
        if (!this.check("In")) {
            return this.failure();
        }

        return super.save(param, files);
    }

    @Override
    protected void beforeUpdate(In entity, Map<String, String> param) {
        for (InList sub : entity.getInLists()) {
            sub.getProduct().getInLists().remove(sub);
            inManager.remove(sub);
        }

        entity.getInLists().clear();
    }

    @Override
    protected void beforeSave(In entity, Map<String, String> param)
        throws Exception {
        param.put("total", param.get("price"));
        this.copyEntity(param, entity);
        inManager.save(entity);

        for (Map.Entry<String, String> entry : param.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if (key.startsWith("sub_id_")) {
                String index = key.substring(7);

                try {
                    Product p = productManager.get(Long.parseLong(value, 10));
                    String name = p.getName();
                    int quantity = Integer.parseInt(param.get("sub_q_"
                                + index), 10);
                    double price = p.getPrice();
                    double subTotal = price * quantity;
                    InList sub = new InList();
                    sub.setProduct(p);
                    sub.setName(name);
                    sub.setQuantity(quantity);
                    sub.setPrice(price);
                    sub.setSubTotal(subTotal);
                    sub.setIn(entity);
                    inManager.save(sub);
                } catch (Exception ex) {
                    System.err.println(ex);
                }
            }
        }
    }

    @Override
    @DirectMethod
    public Object pagedQuery(int start, int limit, String sort,
        String dir, String query) throws Exception {
        if (!this.check("In")) {
            return this.failure();
        }

        return super.pagedQuery(start, limit, sort, dir, query);
    }

    @Override
    @DirectMethod
    public Object remove(String ids) throws Exception {
        if (!this.check("In")) {
            return this.failure();
        }

        return super.remove(ids);
    }

    @Override
    protected void removeById(long id) throws Exception {
        In in = inManager.get(id);
        this.beforeUpdate(in, null);
        this.getEntityDao().removeById(id);
    }
}
